package pers.gxm.strategy.exercise;

public class BubbleSorter implements Sorter{

    @Override
    public void sort(Comparable[] data) {

        boolean flag = false;
        for(int i = 0;i < data.length - 1; i++){
            for(int j = 0;j < data.length - 1 - i ; j++){
                if(data[j].compareTo(data[j + 1]) > 0){
                    Comparable passingPlace = data[j];
                    data[j] = data[j + 1];
                    data[j + 1] = passingPlace;
                    flag = true;
                }
            }
            /**
             * 用来判断提前排序好的情况
             */
            if(!flag){
                System.out.println("一共比较" + (i + 1) + "次");
                break;
            }else{
                flag = false;
            }

        }
    }
}
